package com.zeus.base.smscenter.restful.api.controller;

import com.alibaba.fastjson.JSON;
import com.smscenter.server.core.QueryStrategy;
import com.smscenter.server.core.ems.EmsQuery;
import com.smscenter.server.core.vcomcn.VComQuery;
import com.smscenter.server.core.welink.WelinkQuery;
import com.wordnik.swagger.annotations.ApiOperation;

import com.zeus.base.smscenter.model.SmsSupplierProperty;
import com.zeus.base.smscenter.restful.api.model.ResultModel;
import com.zeus.base.smscenter.restful.api.model.ResultStatus;
import com.zeus.base.smscenter.service.ISupplierPropertyService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.zeus.base.smscenter.common.util.StringUtils;


/**
 * 添加短信帐号
 *
 * @author shanfei
 * @date 2016/06/01.
 */
@RestController
@RequestMapping("/smsquery")
public class QueryController {
    Logger log = Logger.getLogger(this.getClass());
    
    @Autowired
    private ISupplierPropertyService smsSupplierPropertyService;

    @RequestMapping(value = "/queryBalance", method = RequestMethod.POST)
    @ApiOperation(value = "短信帐户余额查询")
    public ResponseEntity<ResultModel> queryBalance(@RequestParam(value = "tenant") String tenant,@RequestParam(value = "password") String password, @RequestParam(value = "supplierChannel") String supplierChannel, @RequestParam(value = "data") String data) {
        //data参数包含 serviceType(即时通短信类型) enterpriseID(鑫易通企业ID),appPlatform应用平台,bs cs 仓配,根据不能发送渠道传相应参数
        if (StringUtils.isEmpty(supplierChannel)){
            log.error("参数:发送渠道系统错误:error:发送渠道不能为空");
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR,"参数:发送渠道系统错误:error:发送渠道不能为空"), HttpStatus.OK);
        }
        if (!supplierChannel.equals("1") && !supplierChannel.equals("2") && !supplierChannel.equals("3")){
            log.error("参数:发送渠道系统错误:error:发送渠道只能为1：及时通,2:EMS,3:微网通联");
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR,"参数:发送渠道系统错误:error:发送渠道只能为1：及时通,2:EMS,3:微网通联"), HttpStatus.OK);
        }
        Map<String, Object> dataparams = JSON.parseObject(data);
        String serviceType = dataparams.get("serviceType")==null ? "":dataparams.get("serviceType").toString();
        String enterpriseID = dataparams.get("enterpriseID")==null ? "":dataparams.get("enterpriseID").toString();
        String appPlatform = dataparams.get("appPlatform")==null ? "":dataparams.get("appPlatform").toString();
        
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("tenant",tenant);
        params.put("password",password);
        params.put("serviceType",serviceType);
        params.put("enterpriseID",enterpriseID);
        params.put("data",data);
        if (!"".equals(appPlatform)) {
        	Map qc = new HashMap();
        	qc.put("code", tenant);
        	qc.put("supplierNo", supplierChannel);
        	qc.put("appPlatform", appPlatform);
            List<SmsSupplierProperty> smsSupplierPropertyList = smsSupplierPropertyService.find(qc);
            for (SmsSupplierProperty smsSupplierProperty : smsSupplierPropertyList) {
                params.put(smsSupplierProperty.getPropertyName(), smsSupplierProperty.getPropertyValue());
            }
		}
        QueryStrategy queryStrategy = null;
        if(supplierChannel.equals("1"))
            queryStrategy = new QueryStrategy(new VComQuery());
        else if(supplierChannel.equals("2"))
            queryStrategy = new QueryStrategy(new EmsQuery());
        else if(supplierChannel.equals("3"))
        	queryStrategy = new QueryStrategy(new WelinkQuery());
        String returnJson = "";
        try {
            returnJson =  queryStrategy.queryBalance(params);
        }catch (Exception e){
            log.error(e.getMessage());
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR, e.getMessage()), HttpStatus.OK);
        }
        return new ResponseEntity(ResultModel.ok(JSON.parse(returnJson)), HttpStatus.OK);
    }

    @RequestMapping(value = "/queryCollect", method = RequestMethod.POST)
    @ApiOperation(value = "短信帐户汇总")
    public ResponseEntity<ResultModel> queryCollect(@RequestParam(value = "tenant") String tenant,@RequestParam(value = "password") String password, @RequestParam(value = "serviceType") String serviceType, @RequestParam(value = "supplierChannel") String supplierChannel, @RequestParam(value = "begin") String begin, @RequestParam(value = "end") String end, @RequestParam(value = "data") String data) {
        if (StringUtils.isEmpty(supplierChannel)){
            log.error("参数:发送渠道系统错误:error:发送渠道不能为空");
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR,"参数:发送渠道系统错误:error:发送渠道不能为空"), HttpStatus.OK);
        }
        if (!supplierChannel.equals("1") && !supplierChannel.equals("2")){
            log.error("参数:发送渠道系统错误:error:发送渠道只能为1：及时通,2:EMS");
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR,"参数:发送渠道系统错误:error:发送渠道只能为1：及时通,2:EMS"), HttpStatus.OK);
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("tenant",tenant);
        params.put("password", password);
        params.put("serviceType",serviceType);
        params.put("begin",begin);
        params.put("end",end);
        params.put("data",data);
        QueryStrategy queryStrategy = null;
        if(supplierChannel.equals("1"))
            queryStrategy = new QueryStrategy(new VComQuery());
        String returnJson = "";
        try {
            returnJson =  queryStrategy.queryCollect(params);
        }catch (Exception e){
            log.error(e.getMessage());
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR, e.getMessage()), HttpStatus.OK);
        }
        return new ResponseEntity(ResultModel.ok(JSON.parse(returnJson)), HttpStatus.OK);
    }
    
    
    @RequestMapping(value = "/querySendRecordInfo", method = RequestMethod.POST)
    @ApiOperation(value = "短信发送记录")
    public ResponseEntity<ResultModel> querySendRecordInfo(@RequestParam(value = "tenant") String tenant, @RequestParam(value = "supplierChannel") String supplierChannel, 
    		@RequestParam(value = "appPlatform") String appPlatform, @RequestParam(value = "data") String data) {
        if (StringUtils.isEmpty(supplierChannel)){
            log.error("参数:发送渠道系统错误:error:发送渠道不能为空");
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR,"参数:发送渠道系统错误:error:发送渠道不能为空"), HttpStatus.OK);
        }
        if (!supplierChannel.equals("3")){
            log.error("参数:发送渠道系统错误:error:发送渠道只能为3:微网通联");
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR,"参数:发送渠道系统错误:error:发送渠道只能为3:微网通联"), HttpStatus.OK);
        }
        
        Map<String, Object> dataparams = JSON.parseObject(data);
        
        Map<String, Object> params = new HashMap<String, Object>();
        params.putAll(dataparams);
        params.put("tenant",tenant);
        
        Map qc = new HashMap();//加载租户的属性
    	qc.put("code", tenant);
    	qc.put("supplierNo", supplierChannel);
    	qc.put("appPlatform", appPlatform);
        List<SmsSupplierProperty> smsSupplierPropertyList = smsSupplierPropertyService.find(qc);
        for (SmsSupplierProperty smsSupplierProperty : smsSupplierPropertyList) {
            params.put(smsSupplierProperty.getPropertyName(), smsSupplierProperty.getPropertyValue());
        }
        
        QueryStrategy queryStrategy = null;
        if(supplierChannel.equals("3"))
            queryStrategy = new QueryStrategy(new WelinkQuery());
        String returnJson = "";
        try {
            returnJson =  queryStrategy.querySendRecordInfo(params);
        }catch (Exception e){
            log.error(e.getMessage());
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR, e.getMessage()), HttpStatus.OK);
        }
        return new ResponseEntity(ResultModel.ok(JSON.parse(returnJson)), HttpStatus.OK);
    }
}

